#!/usr/bin/env bash
tmpargs=''
args=`getopt -o Hhdfes:v: -l help -- "$@"`
if [[ $? != 0 ]]; then
    echo "Terminating..." >&2
    exit 1
fi
eval set -- "$args"
tmpargs="$FDEXCLUDE"
precmd="'head {}'"
while true;do
    case "$1" in
        -h|--help)
            h h fzf
            exit 0;;
        -H)
            tmpargs="$tmpargs -H"
            shift;;
        -d) 
            tmpargs="$tmpargs -t d"
            shift;;
        -f)
            tmpargs="$tmpargs -t f"
            shift;;
        -e)
            tmpargs="$tmpargs --search-path /etc"
            shift;;
        -s)
            tmpargs="$tmpargs --search-path=$2"
            shift 2;;
        -v)
            precmd="'$2 {}'"
            shift 2;;
        --)
            #echo $tmpargs
            shift
            break
            ;;
        *) 
            echo "未知的属性：{$1}"
            exit 1
            ;;
    esac
done
tmpargs="fd $tmpargs | fzf -q $1 --preview=$precmd"
echo $tmpargs |sh
tmpstatus=$?
if [[ $tmpstatus == 0 ]]; then
    unset tmpargs args tmpstatus
    exit 0
else
    unset tmpargs args
    exit $tmpstatus
fi
